package com.ruoyi.common.utils.sms;

import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.config.AliyunSmsConfig;
import com.ruoyi.common.exception.ServiceException;

/**
 * 阿里云短信工具类
 * 
 * @author ruoyi
 */
@Component
public class AliyunSmsUtils
{
    @Autowired
    private AliyunSmsConfig smsConfig;

    /**
     * 发送验证码短信
     * 
     * @param phone 手机号
     * @param code 验证码
     * @return 是否发送成功
     */
    public boolean sendVerifyCode(String phone, String code)
    {
        // 如果未启用短信，仅打印到控制台
        if (!smsConfig.getEnabled())
        {
            System.out.println("========================================");
            System.out.println("【模拟短信】发送验证码");
            System.out.println("手机号：" + phone);
            System.out.println("验证码：" + code);
            System.out.println("有效期：5分钟");
            System.out.println("========================================");
            return true;
        }

        try
        {
            // 创建DefaultProfile
            DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",
                smsConfig.getAccessKeyId(),
                smsConfig.getAccessKeySecret()
            );

            // 创建IAcsClient
            IAcsClient client = new DefaultAcsClient(profile);

            // 构建短信请求
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumbers(phone);
            request.setSignName(smsConfig.getSignName());
            request.setTemplateCode(smsConfig.getTemplateCode());
            request.setTemplateParam("{\"code\":\"" + code + "\"}");

            // 发送短信
            SendSmsResponse response = client.getAcsResponse(request);

            // 判断是否发送成功
            if ("OK".equals(response.getCode()))
            {
                System.out.println("短信发送成功：" + phone + " -> " + code);
                return true;
            }
            else
            {
                System.err.println("短信发送失败：Code=" + response.getCode() + ", Message=" + response.getMessage());
                return false;
            }
        }
        catch (Exception e)
        {
            System.err.println("短信发送异常：" + e.getMessage());
            e.printStackTrace();
            // 开发环境降级：打印验证码
            if (!smsConfig.getEnabled())
            {
                System.out.println("【降级】验证码：" + code);
                return true;
            }
            throw new ServiceException("短信发送失败：" + e.getMessage());
        }
    }

    /**
     * 生成6位随机验证码
     * 
     * @return 验证码
     */
    public String generateVerifyCode()
    {
        Random random = new Random();
        int code = 100000 + random.nextInt(900000);
        return String.valueOf(code);
    }
}
